package com.wsoft.mq.test.config.direct;

import com.wsoft.mq.test.constant.RabbitDirectConstant;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 直连交换机
 * @Author z001
 * @Date 2025/1/7 14:28
 */
@Configuration
public class DirectExchangeConfig {

    @Bean
    public Queue directQueue1() {
        //持久化
        return new Queue(RabbitDirectConstant.DIRECT_QUEUE_1, true);
    }

    /**
     * 定义直连交换机
     * @return
     */
    @Bean
    public DirectExchange directExchange() {
        // 持久化，不自动删除
        return new DirectExchange(RabbitDirectConstant.DIRECT_EXCHANGE, true, false);
    }


    /**
     * 参数可以使用bean名称,但是这个bean名称必须和交换机名称相同,否则会无法找到队列或交换机
     * @param directQueue1
     * @param directExchange
     * @return
     */
    @Bean
    public Binding directBinding1(Queue directQueue1, DirectExchange directExchange) {
        return BindingBuilder.bind(directQueue1())
                .to(directExchange())
                .with(RabbitDirectConstant.DIRECT_ROUTING_KEY_1);
    }
}
